package com.qf.quduba.service.impl;

import com.qf.quduba.dao.RechargeDAO;
import com.qf.quduba.entity.ModulVO;
import com.qf.quduba.entity.Recharge;
import com.qf.quduba.entity.User;
import com.qf.quduba.service.RechargeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 17312
 * @time2020/10/1314:57
 */
@Service
public class RechargeServiceImpl implements RechargeService {
    @Resource
    private RechargeDAO rechargeDAO;
    @Override
    public String addRecharge(Recharge recharge) {
        String rechargeId = UUID.randomUUID().toString().replace("-", "");
        recharge.setRechargeId(rechargeId);
        recharge.setRechargeTime(new Date());
        int i = rechargeDAO.addRecharge(recharge);
        if(i > 0){
            return rechargeId;
        }
        return null;
    }

    @Override
    @Transactional
    public int updateRecharge(String rechargeId) {
        int i = rechargeDAO.updateRecharge(rechargeId);
        Recharge recharge = rechargeDAO.selectRecharge(rechargeId);
        User user1 = rechargeDAO.selectUser(recharge.getUserId());
        User user = new User();
        user.setUserId(recharge.getUserId());
        user.setMoney(recharge.getRechargeAmount()+user1.getMoney());
        rechargeDAO.updateUser(user);
        return i;

    }

    @Override
    public List<Recharge> findUserRecharge(String userId) {
        int userId1 = Integer.parseInt(userId);
        return rechargeDAO.findUserRecharge(userId1);
    }

    @Override
    @Transactional
    public int deleteRecharges(ModulVO modulVO) {
        int i = 0;
        for (String rechargeId : modulVO.getRechargeIds()) {
            i = rechargeDAO.deleteRecharge(rechargeId);
        }
        return i;
    }

    @Override
    public int deleteRecharge(String rechargeId) {
        return rechargeDAO.deleteRecharge(rechargeId);
    }


}
